package com.voxel.sense.infomsg.module.task.send.firmlist;

import android.text.TextUtils;
import android.view.View;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.utils.L;
import com.example.com.common.utils.ToastUtil;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.utils.GlideUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.Factory;
import cyy.example.com.facotry.model.api.response.FirmNameByIdEntity;

/**
 * 菜单筛选栏的适配器
 * Created by ChenYiYao on 2018/8/10.
 */
public class FirmPickRecyclerAdapter extends RecyclerAdapter<FirmNameByIdEntity> implements Filterable {

    //是否单选
    private boolean mIsSingleSelect = false;
    //单选时，用于记录选中项
    private int current_position = -1;
    private int mMaxCount = -1; //最大选择数量
    private List<FirmNameByIdEntity> mSourceList;
    private List<FirmNameByIdEntity> mSeletedDatas = new ArrayList<>();//已选中的数据

    public FirmPickRecyclerAdapter(List<FirmNameByIdEntity> sourceList) {
        mSourceList = sourceList;
    }

    @Override
    protected int getItemViewType(int position, FirmNameByIdEntity firmNameByIdEntity) {
        return R.layout.item_recyler_select_tip;
    }

    @Override
    protected RecyclerAdapter.ViewHolder<FirmNameByIdEntity> onCreateViewHolder(View root, int viewType) {
        return new ViewHolder(root);
    }

    @Override
    public void onBindViewHolder(final RecyclerAdapter.ViewHolder<FirmNameByIdEntity> holder, final int position) {
        super.onBindViewHolder(holder, position);
//        增加点击事件
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterStatu(position);

                if (mListener != null) {
                    mListener.onItemClick(holder, mDataList.get(position));
                }
            }
        });
    }

    /**
     * 改变数据状态
     *
     * @param position
     */
    protected void alterStatu(int position) {
        FirmNameByIdEntity bean = mDataList.get(position);
        boolean select = !bean.isSelect();
        if (select && (mMaxCount != -1 && mSeletedDatas.size() >= mMaxCount)) {
            //TODO: 已达最大数据
            ToastUtil.showToastLong(Factory.app(), "最多只能选择五家企业");
            return;
        } else if (select) {
            mSeletedDatas.add(bean);

        } else {
            mSeletedDatas.remove(bean);

        }

        bean.setSelect(select);
        notifyItemChanged(position);
        if (mIsSingleSelect) {
            if (current_position != -1 && position != current_position) {
                //改变之前数据的状态
                L.d("");
                FirmNameByIdEntity oldBea = mDataList.get(current_position);
                oldBea.setSelect(false);
                notifyItemChanged(current_position);
            }
            current_position = position;
        }


    }

    public List<FirmNameByIdEntity> getSeletedDatas() {
        return mSeletedDatas;
    }

    public void setSeletedDatas(List<FirmNameByIdEntity> seletedDatas) {
        mSeletedDatas = seletedDatas;
    }

    /**
     * 重置数据状态
     */
    public void reSetStatus() {
        ArrayList list = new ArrayList();
        boolean flag = false;
        for (FirmNameByIdEntity bean : mDataList) {
            if (bean.isSelect()) {
                bean.setSelect(false);
                flag = true;
            }
        }

        if (flag) {
            notifyDataSetChanged();
        }
    }

    public boolean isSingleSelect() {
        return mIsSingleSelect;
    }

    public void setSingleSelect(boolean singleSelect) {
        mIsSingleSelect = singleSelect;
    }

    public int getMaxCount() {
        return mMaxCount;
    }

    public List<FirmNameByIdEntity> getSourceList() {
        return mSourceList;
    }

    public void setSourceList(List<FirmNameByIdEntity> sourceList) {
        mSourceList = sourceList;
    }

    public void setMaxCount(int maxCount) {
        mMaxCount = maxCount;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                final ArrayList<FirmNameByIdEntity> newValues = new ArrayList<FirmNameByIdEntity>();
                if (TextUtils.isEmpty(constraint)) {
                    results.count = mSourceList.size();
                    results.values = mSourceList;
                } else {
                    for (int i = 0; i < mSourceList.size(); i++) {
                        FirmNameByIdEntity firmNameByIdEntity = mSourceList.get(i);
                        if (TextUtils.isEmpty(firmNameByIdEntity.companyName) || firmNameByIdEntity.companyName.contains(constraint)) {
                            newValues.add(firmNameByIdEntity);
                        }
                    }
                    results.count = newValues.size();
                    results.values = newValues;
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                replace((Collection<FirmNameByIdEntity>) results.values);
            }
        };
    }


    public static class ViewHolder extends RecyclerAdapter.ViewHolder<FirmNameByIdEntity> {
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.iv_select_icon)
        ImageView iv_select_icon;

        public ViewHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(FirmNameByIdEntity bean) {
            tv_desc.setText(bean.getCompanyName());
            if (bean.isSelect()) {
                GlideUtils.GlideLoadPicNoDefault(iv_select_icon.getContext(), R.mipmap.map_tip_select_icon, iv_select_icon);
            } else {
                GlideUtils.GlideLoadPicNoDefault(iv_select_icon.getContext(), R.mipmap.map_tip_unselect_icon, iv_select_icon);
            }
        }
    }

}

